package com.eqdd.yiqidian.ui.mine.shenqing;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Parcelable;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.amap.api.maps2d.model.LatLng;
import com.bigkoo.pickerview.TimePickerView;
import com.eqdd.annotation.datasave.ToSave;
import com.eqdd.nextinputs.StaticScheme;
import com.eqdd.yiqidian.ApplayLizhiActivityCustom;
import com.eqdd.yiqidian.R;
import com.eqdd.yiqidian.adapter.slimadapter.SlimInjector;
import com.eqdd.yiqidian.adapter.slimadapter.viewinjector.IViewInjector;
import com.eqdd.yiqidian.base.Config;
import com.eqdd.yiqidian.bean.rebuild.slimedit.SlimEditApplyTitleBean;
import com.eqdd.yiqidian.bean.rebuild.slimedit.SlimEditEnterBean;
import com.eqdd.yiqidian.bean.rebuild.slimedit.SlimEditHintBean;
import com.eqdd.yiqidian.bean.rebuild.slimedit.SlimTwoSectionEditBean;
import com.eqdd.yiqidian.http.DialogCallBack;
import com.eqdd.yiqidian.http.HttpConfig;
import com.eqdd.yiqidian.http.HttpResult;
import com.eqdd.yiqidian.map.SelectMapActivity;
import com.eqdd.yiqidian.ui.base.SlimBaseMultiEditActivity;
import com.eqdd.yiqidian.utils.SingleChooseUtil;
import com.eqdd.yiqidian.utils.ToastUtil;
import com.eqdd.yiqidian.widget.ActionSheetDialog;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class ApplyTravelActivity extends SlimBaseMultiEditActivity {
    private ApplayLizhiActivityCustom dataBinding;

    private String msg;
    private int clickPos;
    private boolean isUpper;
    private SimpleDateFormat sf;
    @ToSave
    String latlon;

    @Override
    public void initBinding() {
        dataBinding = DataBindingUtil.setContentView(this, R.layout.activity_applay_lizhi);
        initTopTitleBar(View.VISIBLE, "出差申请");
        initTopRightText("提交", v -> {
            if (checkParam()) {
                SlimTwoSectionEditBean from = (SlimTwoSectionEditBean) slimAdapterEx.getDataItem(1);
                OkGo.<HttpResult>post(HttpConfig.BASE_URL + HttpConfig.CREATE_TRAVEL)
                        .params("companyId", user.getCompanyId())
                        .params("userGuid", user.getGuid())
                        .params("travelStartTime", from.getUpContent())
                        .params("travelEndTime", from.getDownContent())
                        .params("travelTimes", setParam(2))
                        .params("travelReason", setParam(3))
                        .params("travelAddress", setParam(4))
                        .params("mapAddress", dealText(latlon))
                        .execute(new DialogCallBack<HttpResult>(this) {
                            @Override
                            public void onSuccess(Response<HttpResult> response) {
                                HttpResult httpResult = response.body();
                                ToastUtil.showShort(httpResult.getMsg());
                                if (httpResult.getStatus() == 200) {
                                    isSave=false;
                                    clearData();
                                    setResult(Config.SUCCESS);
                                    finish();
                                }
                            }
                        });
            } else {
                ToastUtil.showShort(msg);
            }
        });
    }

    private boolean checkParam() {
        SlimTwoSectionEditBean from = (SlimTwoSectionEditBean) slimAdapterEx.getDataItem(1);

        if (TextUtils.isEmpty(from.getUpContent()) || TextUtils.isEmpty(from.getDownContent())) {
            msg = "请完善出差时间";

            return false;
        }
        if (TextUtils.isEmpty(setParam(2))) {
            msg = "请输入出差时长";
            return false;
        }
        if (TextUtils.isEmpty(setParam(3))) {
            msg = "请输入出差原因";
            return false;
        }
        if (TextUtils.isEmpty(setParam(4))) {
            msg = "请选择出差地点";
            return false;
        }
        return true;
    }


    @Override
    protected RecyclerView initRecyclerView() {
        sf = new SimpleDateFormat("yyyy-MM-dd HH:mm");

        return dataBinding.recyclerView;
    }

    @Override
    public void itemClick(int p) {
        switch (p) {
            case 4:
                startActivityForResult(SelectMapActivity.class, null, Config.SELECT_MAP);
                break;
        }
    }

    @Override
    protected void setEditBeans() {
        slimAdapterEx.register(R.layout.list_item_42, new SlimInjector<SlimTwoSectionEditBean>() {
            @Override
            public void onInject(SlimTwoSectionEditBean data, IViewInjector injector) {
                injector.text(R.id.tv_upper_left_content, data.getUpTitle())
                        .text(R.id.tv_upper_right_content, data.getUpContent())
                        .text(R.id.tv_down_left_content, data.getDownTitle())
                        .text(R.id.tv_down_right_content, data.getDownContent())
                        .gone(R.id.stv_bottom)
                        .with(R.id.tv_upper_right_content, v -> ((TextView) v).setHint("请选择"))
                        .with(R.id.tv_down_right_content, v -> ((TextView) v).setHint("请选择"))
                        .clicked(R.id.rl_upper, v -> {
                            clickPos = slimAdapterEx.getData().indexOf(data);
                            isUpper = true;
                            openTimePicker();
                        })
                        .clicked(R.id.rl_down, v -> {
                            clickPos = slimAdapterEx.getData().indexOf(data);
                            isUpper = false;
                            openTimePicker();
                        });

            }
        });

        datas.add(new SlimEditApplyTitleBean());
        datas.add(new SlimTwoSectionEditBean("", "", 1,
                "出差开始时间", "出差结束时间", "", ""));
        datas.add(new SlimEditEnterBean("出差时长", "", 2, "请输入").addSchemes(StaticScheme.Numeric()));
        datas.add(new SlimEditEnterBean("出差原因", "", 3, "请输入"));
        datas.add(new SlimEditEnterBean("出差地点", "", 4, true, "请选择"));
        datas.add(new SlimEditHintBean("审批人", "", 5));
    }

    private void openTimePicker() {
        Calendar now = Calendar.getInstance();
        now.setTime(new Date());
        TimePickerView pvDayTime = new TimePickerView.Builder(this, (date, v) -> {

            SlimTwoSectionEditBean dataItem = (SlimTwoSectionEditBean) slimAdapterEx.getDataItem(clickPos);

            if (isUpper) {
                dataItem.setUpContent(sf.format(date));
                slimAdapterEx.notifyItemChanged(clickPos);
            } else {
                dataItem.setDownContent(sf.format(date));
                slimAdapterEx.notifyItemChanged(clickPos);
            }
        })
                .setOutSideCancelable(true)
                .setType(new boolean[]{true, true, true, true, true, false})
                .setRange(now.get(Calendar.YEAR), 2050)
                .setDate(now)
                .build();
        pvDayTime.show();
    }

    @Override
    protected void loadNetData() {

        OkGo.<HttpResult<String>>post(HttpConfig.BASE_URL + HttpConfig.GET_LEADER)
                .params("userGuid", user.getGuid())
                .params("companyId", user.getCompanyId())
                .execute(new DialogCallBack<HttpResult<String>>(ApplyTravelActivity.this) {
                    @Override
                    public void onSuccess(Response<HttpResult<String>> response) {
                        HttpResult<String> httpResult = response.body();
                        ToastUtil.showShort(httpResult.getMsg());
                        if (httpResult.getStatus() == 200) {
                            slimAdapterEx.getData().get(5).setContent(httpResult.getItems());
                            slimAdapterEx.notifyItemChanged(5);
                        }
                    }
                });
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {

        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == Config.SUCCESS && requestCode == Config.SELECT_MAP) {
            String address = data.getStringExtra(Config.MAP_RESULT_STRING);
            LatLng latLng = data.getParcelableExtra(Config.MAP_RESULT_LOCATION);
            latlon = latLng.latitude + "," + latLng.longitude;
            slimAdapterEx.getDataItem(4).setContent(address);
            slimAdapterEx.notifyItemChanged(4);
        } else {
            super.onActivityResult(requestCode, resultCode, data);
        }
    }
}
